package service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.CommentDao;
import dao.VideoDao;
import entity.Comment;
import entity.Video;
import service.CommentService;
import util.PageUtil;

@Service
public class CommentServiceImpl implements CommentService {

	@Autowired
	SqlSessionFactory factory;
	@Autowired
	PageUtil<Comment> pageUtilComment;
	
	@Override
	public PageUtil<Comment> fingByVideoId(int videoId, int pageIndex, int pageSize) {
		SqlSession sqlSession=null;
		System.out.println(videoId + "-" + pageIndex + "-" + pageSize);
		try {
			sqlSession=factory.openSession();
			CommentDao dao = sqlSession.getMapper(CommentDao.class);
			int count = dao.fingCountByVideoId(videoId);
			//num:跳过多少条数据
			 int num=(pageIndex-1)*pageSize;
			 List<Comment> list = dao.fingByVideoId(videoId, num, pageSize);
			 
			pageUtilComment.setList(list);
			pageUtilComment.setCount(count);
			pageUtilComment.setPageIndex(pageIndex);
			pageUtilComment.setPageSize(pageSize);
			pageUtilComment.setPageNumber((int)Math.ceil((double)count/pageSize));
			
			return pageUtilComment;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return null;
	}

	@Override
	public PageUtil<Comment> findByListId(int listId, int pageIndex, int pageSize) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			CommentDao dao = sqlSession.getMapper(CommentDao.class);
			//num:跳过多少条数据
			int num = (pageIndex-1)*pageSize;
			int count = dao.findCountByListId(listId);
			List<Comment> list = dao.findByListId(listId, num, pageSize);
			
			pageUtilComment.setList(list);
			pageUtilComment.setCount(count);
			pageUtilComment.setPageIndex(pageIndex);
			pageUtilComment.setPageSize(pageSize);
			pageUtilComment.setPageNumber((int)Math.ceil((double)count/pageSize));
			
			return pageUtilComment;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return null;
	}

	@Override
	public int add(Comment comment) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			CommentDao dao = sqlSession.getMapper(CommentDao.class);
			int count = dao.add(comment);
			sqlSession.commit();
			return count;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return 0;
	}

	@Override
	public int updateCommState(int type, int commId) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			CommentDao dao = sqlSession.getMapper(CommentDao.class);
			int count = dao.updateCommState(type, commId);
			sqlSession.commit();
			return count;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return 0;
	}

	@Override
	public List<Comment> findByState(int stateId) {
		SqlSession sqlSession=null;
		try {
			sqlSession=factory.openSession();
			CommentDao dao = sqlSession.getMapper(CommentDao.class);
			List<Comment> list = dao.findByState(stateId);
			return list;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			sqlSession.close();
		}
		return null;
	}


}
